﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Guline.SupportCMS.Models.Object;
using Guline.SupportCMS.Models.Context;

namespace Guline.SupportCMS.Controllers.Manage
{
    public class CategoryController : GulineBaseController
    {

        public ActionResult jsonList(string ParentID)
        {
            int mParentID = 0;
            try
            {
                mParentID = int.Parse(ParentID);

            }
            catch { 

            }
            if (mParentID != 0)
            {
                var cats = from m in db.Categories where m.ParentID == mParentID select new { m.ID,m.Name,ParentName=m.Parent.Name,m.Order,m.Enable};
                return Json(cats, JsonRequestBehavior.AllowGet);
            }
            else
            {
                var cats = from m in db.Categories where m.ParentID == null select new { m.ID, m.Name, ParentName = m.Parent.Name, m.Order, m.Enable };
                return Json(cats, JsonRequestBehavior.AllowGet);
            }
        }
        public ActionResult Index()
        {
            if(!string.IsNullOrWhiteSpace(Request.QueryString["ParentID"]))
            {
                int id= int.Parse(Request.QueryString["ParentID"]);
                var category = db.Categories.Where(m => m.ID ==id ).Include(m => m.Parent).FirstOrDefault();
                ViewBag.Category = category;
            }
            return View();
        }

        //
        // GET: /Category/Details/5

        public ActionResult Details(int id = 0)
        {
            Category category = db.Categories.Find(id);
            if (category == null)
            {
                return HttpNotFound();
            }
            return View(category);
        }

        //
        // GET: /Category/Create

        public ActionResult Create()
        {
            ViewBag.ParentID = new SelectList(db.Categories.Where(m => m.ParentID == null), "ID", "Name");
            return View();
        }

        //
        // POST: /Category/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Category category)
        {
            if (ModelState.IsValid)
            {
                db.Categories.Add(category);
                db.SaveChanges();
                return RedirectToAction("Index", new { ParentID = category.ParentID });
            }

            ViewBag.ParentID = new SelectList(db.Categories.Where(m=>m.ParentID==null), "ID", "Name", category.ParentID);
            return View(category);
        }

        //
        // GET: /Category/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Category category = db.Categories.Find(id);
            if (category == null)
            {
                return HttpNotFound();
            }
            ViewBag.ParentID = new SelectList(db.Categories.Where(m => m.ParentID == null), "ID", "Name", category.ParentID);
            return View(category);
        }

        //
        // POST: /Category/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Category category)
        {
            if (ModelState.IsValid)
            {
                db.Entry(category).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index", new { ParentID = category.ParentID });
            }
            ViewBag.ParentID = new SelectList(db.Categories.Where(m => m.ParentID == null), "ID", "Name", category.ParentID);
            return View(category);
        }

        //
        // GET: /Category/Delete/5
        public ActionResult ajDelete(int id = 0)
        {
            var articlecategory = db.ArticleCategories.Where(m => m.CategoryID == id);
            foreach(var ac in articlecategory)
            {
                db.ArticleCategories.Remove(ac);
            }
            db.SaveChanges();
            var videocategory = db.VideoCategorys.Where(m => m.CategoryID == id);
            foreach (var av in videocategory)
            {
                db.VideoCategorys.Remove(av);
            }
            db.SaveChanges();
            Category category = db.Categories.Find(id);
            db.Categories.Remove(category);
            db.SaveChanges();
            return Json(new { success = true });
        }
        public ActionResult Delete(int id = 0)
        {
            Category category = db.Categories.Find(id);
            if (category == null)
            {
                return HttpNotFound();
            }
            return View(category);
        }

        //
        // POST: /Category/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Category category = db.Categories.Find(id);
            db.Categories.Remove(category);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}